package com.example.signin.uitl;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.signin.R;


public class MyStatisticsView extends View {
    private Context mContext;
    /**
     * 背景画笔
     **/
    private Paint mBgPaint;

    /**
     * 画笔
     **/
    private Paint mPaint;

    private int mMaxWidth;
    private int mMaxHeight;

    /**
     * 颜色
     **/
    private int selectColor = 0;
    /**
     * 值
     **/
    private float value = 0;

    public MyStatisticsView(Context context) {
        super(context, null);
        mContext = context;
    }

    public MyStatisticsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        mContext = context;
        TypedArray ta = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MyStatisticsView, 0, 0);
        if (ta != null) {
            selectColor = ta.getColor(R.styleable.MyStatisticsView_select_color, 0);
            value = ta.getInt(R.styleable.MyStatisticsView_value, 0);
        }
        ta.recycle();
        init();
    }

    public MyStatisticsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mMaxWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMaxHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    private void init() {
        mBgPaint = new Paint();
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setAntiAlias(true);
        //背景颜色（自己定义）
        mBgPaint.setColor(Color.parseColor("#f0f0f0"));

        mPaint = new Paint();
        mPaint.setColor(selectColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        deawBG(canvas);
        deawValue(canvas);
    }

    /**
     * 画背景
     */
    private void deawBG(Canvas canvas) {
        if (mBgPaint == null) {
            return;
        }
        RectF rectF = new RectF();
        rectF.top = 0;
        rectF.left = 0;
        rectF.bottom = mMaxHeight;
        rectF.right = mMaxWidth;
        canvas.drawRoundRect(rectF, mMaxWidth / 2, mMaxWidth / 2, mBgPaint);
    }

    /**
     * 画值
     */
    private void deawValue(Canvas canvas) {
        if (mPaint == null) {
            return;
        }
        RectF rectF = new RectF();
        rectF.left = 0;
        rectF.top = mMaxHeight / 100 * (100 - value);
        rectF.bottom = mMaxHeight;
        rectF.right = mMaxWidth;
        canvas.drawRoundRect(rectF, mMaxWidth / 2, mMaxWidth / 2, mPaint);
    }

    public void setData(float value) {
        this.value = value;
        invalidate();
    }

    public void setColor(int color) {
        this.selectColor = color;
        invalidate();
    }
}
